﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// Summary description for LoaiTour
/// </summary>
public class LoaiTour
{
    SqlCommand cmd;
    SqlDataReader dr;
	public LoaiTour()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public string getDanhMucTour()
    {
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);

        sqlConnection.Open();
        cmd = new SqlCommand("sp_LoaiTourGetDanhMuc", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        dr = cmd.ExecuteReader();
        string html = "";
        Utils utils = new Utils();
        string ten_trang = "";
        while (dr.Read())
        {
            ten_trang = Utils.GetTitle( dr["ten_loai_tour"].ToString());
            html += "<li><a href=\"/tour-list/" + dr["loai_tour_id"].ToString() + "/"+ten_trang+".html\">" + dr["ten_loai_tour"].ToString() + "</a></li>";
        }

        dr.Close();
        sqlConnection.Close();
        return html;
    }

    public List<ObjLoaiTour> getDanhMucTour(string type)
    {
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);

        sqlConnection.Open();
        cmd = new SqlCommand("sp_LoaiTourGetTypeHien", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter[] sqla = new SqlParameter[1];
        sqla[0]=new SqlParameter("type", DbType.String);
        sqla[0].Value = type;
        cmd.Parameters.AddRange(sqla);
        dr = cmd.ExecuteReader();
        ObjLoaiTour objLoaiTour;
        List<ObjLoaiTour> lst = new List<ObjLoaiTour>();
        while (dr.Read())
        {
            objLoaiTour = new ObjLoaiTour();
            objLoaiTour.Loai_tour_id = int.Parse(dr["loai_tour_id"].ToString());
            objLoaiTour.Ten_loai_tour = dr["ten_loai_tour"].ToString();
            lst.Add(objLoaiTour);
        }

        dr.Close();
        sqlConnection.Close();
        return lst;
    }

    public ObjLoaiTour getLoaiTourById(String loai_tour_id)
    {
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);

        sqlConnection.Open();
        cmd = new SqlCommand("sp_LoaiTourGetById", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter[] sqla = new SqlParameter[1];
        sqla[0] = new SqlParameter("@Loai_tour_id", DbType.String);
        sqla[0].Value = loai_tour_id;
        cmd.Parameters.AddRange(sqla);
        dr = cmd.ExecuteReader();
        ObjLoaiTour objLoaiTour = new ObjLoaiTour();
        if (dr.Read())
        {
            objLoaiTour.Ten_loai_tour = dr["ten_loai_tour"].ToString();
            objLoaiTour.Hien_danh_muc = dr["hien_danh_muc"].ToString();
            //objLoaiTour.Hien_trang_chu = int.Parse(dr["hien_trang_chu"].ToString());
            objLoaiTour.Vi_tri = Int16.Parse(dr["vi_tri"].ToString());
            objLoaiTour.Loai_tour_cha_id = int.Parse(dr["loai_tour_cha_id"].ToString());
        }

        dr.Close();
        sqlConnection.Close();
        return objLoaiTour;
        
    }

    public List<ObjLoaiTour> getAllDanhMucTour()
    {
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);

        sqlConnection.Open();
        cmd = new SqlCommand("sp_LoaiTourGetDanhMucChaCon", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        
        dr = cmd.ExecuteReader();
        ObjLoaiTour objLoaiTour;
        List<ObjLoaiTour> lst = new List<ObjLoaiTour>();
        while (dr.Read())
        {
            objLoaiTour = new ObjLoaiTour();
            objLoaiTour.Ten_loai_tour = dr["ten_loai_tour"].ToString();
            objLoaiTour.Loai_tour_id = int.Parse(dr["loai_tour_id"].ToString());
            objLoaiTour.Vi_tri = int.Parse(dr["vi_tri"].ToString());
           
            lst.Add(objLoaiTour);
        }

        dr.Close();
        sqlConnection.Close();
        return lst; 

    }
    public List<ObjLoaiTour> getDanhMucTourSearch()
    {
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);

        sqlConnection.Open();
        cmd = new SqlCommand("sp_LoaiTourGetDanhMucSearch", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;

        dr = cmd.ExecuteReader();
        ObjLoaiTour objLoaiTour;
        List<ObjLoaiTour> lst = new List<ObjLoaiTour>();
        while (dr.Read())
        {
            objLoaiTour = new ObjLoaiTour();
            objLoaiTour.Ten_loai_tour = dr["ten_loai_tour"].ToString();
            objLoaiTour.Loai_tour_id = int.Parse(dr["loai_tour_id"].ToString());
            objLoaiTour.Vi_tri = int.Parse(dr["vi_tri"].ToString());

            lst.Add(objLoaiTour);
        }

        dr.Close();
        sqlConnection.Close();
        return lst;

    }

    public void createLoaiTour(ObjLoaiTour objLoaiTour)
    {
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);

        sqlConnection.Open();
        cmd = new SqlCommand("sp_LoaiTourCreate", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter[] sqla = new SqlParameter[5];
        sqla[0] = new SqlParameter("ten_loai_tour", DbType.String);
        sqla[1] = new SqlParameter("Hien_trang_chu", DbType.Int16);
        sqla[2] = new SqlParameter("hien_danh_muc", DbType.Int16);
        sqla[3] = new SqlParameter("vi_tri", DbType.Int16);
        sqla[4] = new SqlParameter("loai_tour_cha_id", DbType.Int16);

        sqla[0].Value = objLoaiTour.Ten_loai_tour;
        sqla[1].Value = objLoaiTour.Hien_trang_chu;
        sqla[2].Value = objLoaiTour.Hien_danh_muc;
        sqla[3].Value = objLoaiTour.Vi_tri;
        sqla[4].Value = objLoaiTour.Loai_tour_cha_id;

        cmd.Parameters.AddRange(sqla);
        cmd.ExecuteNonQuery();
        sqlConnection.Close();
    }

    public void updateLoaiTour(ObjLoaiTour objLoaiTour)
    {
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);

        sqlConnection.Open();
        cmd = new SqlCommand("sp_LoaiTourUpdate", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter[] sqla = new SqlParameter[6];
        sqla[0] = new SqlParameter("ten_loai_tour", DbType.String);
        sqla[1] = new SqlParameter("Hien_trang_chu", DbType.Int16);
        sqla[2] = new SqlParameter("hien_danh_muc", DbType.Int16);
        sqla[3] = new SqlParameter("vi_tri", DbType.Int16);
        sqla[4] = new SqlParameter("loai_tour_id", DbType.Int32);
        sqla[5] = new SqlParameter("loai_tour_cha_id", DbType.Int32);

        sqla[0].Value = objLoaiTour.Ten_loai_tour;
        sqla[1].Value = objLoaiTour.Hien_trang_chu;
        sqla[2].Value = objLoaiTour.Hien_danh_muc;
        sqla[3].Value = objLoaiTour.Vi_tri;
        sqla[4].Value = objLoaiTour.Loai_tour_id;
        sqla[5].Value = objLoaiTour.Loai_tour_cha_id;
        cmd.Parameters.AddRange(sqla);
        cmd.ExecuteNonQuery();
        sqlConnection.Close();
    }


    public void updateLoaiTourByTrangThai(ObjLoaiTour objLoaiTour)
    {
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);

        sqlConnection.Open();
        cmd = new SqlCommand("sp_LoaiSanPPhamUpdataByTrangThai", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter[] sqla = new SqlParameter[2];
        sqla[0] = new SqlParameter("loai_san_pham_id", DbType.Int32);
        sqla[1] = new SqlParameter("@Trang_thai", DbType.Int16);

        sqla[0].Value = objLoaiTour.Loai_tour_id;
        sqla[1].Value = objLoaiTour.Trang_thai;
        
        cmd.Parameters.AddRange(sqla);
        cmd.ExecuteNonQuery();
        sqlConnection.Close();
        sqlConnection.Dispose();
    }

    public void deleteLoaiTourByTrangThai(ObjLoaiTour objLoaiTour)
    {
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);

        sqlConnection.Open();
        cmd = new SqlCommand("sp_LoaiTourDelete", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter[] sqla = new SqlParameter[1];
        sqla[0] = new SqlParameter("loai_tour_id", DbType.Int32);
        

        sqla[0].Value = objLoaiTour.Loai_tour_id;
        

        cmd.Parameters.AddRange(sqla);
        cmd.ExecuteNonQuery();
        sqlConnection.Close();
        sqlConnection.Dispose();
    }

    public void deleteLoaiTourByXml(string xml)
    {
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);

        sqlConnection.Open();
        cmd = new SqlCommand("sp_LoaiTourDeleteByXml", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter[] sqla = new SqlParameter[1];
        sqla[0] = new SqlParameter("Doc", DbType.String);
        sqla[0].Value = xml;
        cmd.Parameters.AddRange(sqla);
        cmd.ExecuteNonQuery();
        sqlConnection.Close();
        sqlConnection.Dispose();

    }

    public void updateViTriLoaiTourByXml(string xml)
    {
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);

        sqlConnection.Open();
        cmd = new SqlCommand("sp_LoaiSanPhamUpdateViTriByXml", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter[] sqla = new SqlParameter[1];
        sqla[0] = new SqlParameter("Doc", DbType.String);
        sqla[0].Value = xml;
        cmd.Parameters.AddRange(sqla);
        cmd.ExecuteNonQuery();
        sqlConnection.Close();
        sqlConnection.Dispose();

    }


    public List<ObjLoaiTour> getLoaiTourByLoaiTourChaId(int id)
    {
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);

        sqlConnection.Open();
        cmd = new SqlCommand("sp_LoaiTourGetByLoaiTourChaID", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter[] sqla = new SqlParameter[1];
        sqla[0] = new SqlParameter("Loai_tour_cha_id", DbType.Int32);
        sqla[0].Value = id;
        cmd.Parameters.AddRange(sqla);
        dr = cmd.ExecuteReader();
        ObjLoaiTour objLoaiTour;
        List<ObjLoaiTour> lst = new List<ObjLoaiTour>();
        while (dr.Read())
        {
            objLoaiTour = new ObjLoaiTour();
            objLoaiTour.Ten_loai_tour = dr["ten_loai_tour"].ToString();
            objLoaiTour.Loai_tour_id = int.Parse(dr["loai_tour_id"].ToString());
            lst.Add(objLoaiTour);
        }

        dr.Close();
        sqlConnection.Close();
        return lst;
    }


    public string getLeftMenu()
    {
        string html="";

        List<ObjLoaiTour> lst = getLoaiTourByLoaiTourChaId(0);
        foreach (ObjLoaiTour item in lst)
        {
             html+= "<li class=\"lstc\" >";

            html += "<a href=\"/tour-list/" + item.Loai_tour_id + "/" + Utils.GetTitle( item.Ten_loai_tour) + ".html\">" + item.Ten_loai_tour + "</a>";
            html += getMenuTrungGian(item.Loai_tour_id);
            html += "</li>";
        }
       
       
        return html;
    }
    //lay meu con
    public string getMenuTrungGian(int id)
    {
        string html = "<ul>";
        bool flag = false;//bien xac dinh co danh menu cap con hay khong
        List<ObjLoaiTour> lst = new List<ObjLoaiTour>();
        lst = getLoaiTourByLoaiTourChaId(id);
        foreach (ObjLoaiTour item in lst)
        {
            html += "<li><a href=\"/tour-list/" + item.Loai_tour_id + "/" + Utils.GetTitle( item.Ten_loai_tour )+ ".html\">" + item.Ten_loai_tour + "</a>";
            html += getMenuTrungGian(item.Loai_tour_id);
            html+="</li>";
        }
        html += "</ul>";
        if(lst.Count==0)
            html = "";
        return html;
    }

    
}
